<?php
//商品属性
class Controller_Admin_GoodsAttr extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'goodsAttr';
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );


        $nid = $this->request->query('goodsId');
        if ($nid > 0)
            $this->_goods_nid = $nid;
        else
            throw new Exception("无效商品参数");
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//首页
    public function action_goods ()
    {
        $this->action_list();
    }

	//属性删除
    public function action_delete ($id)
    {
        $goodsNid = App_Goods_Attr::remove($id);
        $this->_goods_nid = $goodsNid;
        $this->to_url('goods');
    }

	//添加属性
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $post['attr1Id'] = Arr::get($post['attrId'], 0);
            $post['attr2Id'] = Arr::get($post['attrId'], 1);
            $post['attr3Id'] = Arr::get($post['attrId'], 2);

            $this->_form_data = $post;
            App_Goods_Attr::add($this->_goods_nid, $this->_form_data);
            $this->to_url('goods');
        }
        else
        {
            parent::action_create();
        }
    }

	//修改属性信息
	//$nid int 属性id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $post['attr1Id'] = Arr::get($post['attrId'], 0);
            $post['attr2Id'] = Arr::get($post['attrId'], 1);
            $post['attr3Id'] = Arr::get($post['attrId'], 2);
            $this->_form_data = $post;
 
            App_Goods_Attr::update($nid, $this->_form_data);
            $this->to_url('goods');
        }
        else
        {
            $data = App_Goods_Attr::detail($nid);
            if (empty($data['attr']))
                $data['attr'] = array(0 => array( 'parentId' => 0, 'attrId' => 0 ));

            $data['price'] = number_format($data['price'] / 100, 2);
            $this->_form_data = $data;
            parent::action_update($nid);
        }
    }

	//属性详细
	//$nid int 属性id
    public function action_detail ($nid)
    {
        $data = App_CategoryGL::detail($nid);
        $this->_form_data = $data;

        parent::action_detail($nid);
    }

	//数据重构
    private function rebuild_post_data (& $post)
    {
        $post['goodsId'] = $this->_goods_nid;
        $post['uid']   = user::current_user_id();
        $post['price'] = $post['price'] * 100;
        $post['positionNumber'] = trim(Arr::get($post, 'positionNumber', ''));
        $post['barCode'] = trim(Arr::get($post, 'barCode', ''));
    }

	//数据格式改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['price'] = '￥'.number_format($rs['price'] / 100, 2);
        }
    }

    public function list_get_data ($offset, $limit)
    {
        return App_Goods_Attr::infoList($this->_goods_nid, $offset, $limit);
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        array_shift($menus);
    }
}

